import {FCTable} from "@components";

import React, { useEffect, useState } from 'react';

import { DetailsModelBodyHeight, getDetailsTableColumns } from "../config";

import { findEmerNotifierInnerDetail } from '@apis/emergencyInformApi';
import { notification } from "antd";

const DetailsTable = ({height, emerMasterId}) => {
    const [loading, setLoading] = useState(false);
    const [dataSource, setDataSource] = useState([])
    const [pagiProps, setPagiProps] = useState({ current: 1, pageSize: 20 })

    const getDataSource = async (pageProps) => {
      setLoading(true);
      const {pageSize, current: currentPage} = pageProps ?? pagiProps
      const { data, error } = await findEmerNotifierInnerDetail({queryDto: { emerMasterId }, currentPage, pageSize});
      setLoading(false);
  
      if (error) return; setDataSource([]);
      if (!data.pageList?.length) {
        notification.info({
          message: '温馨提示',
          description:'请检查该机构的应急通知配置是否已配置对应风险等级的通知模版及角色',
          duration: 8,
        });
      }
      setPagiProps({ ...pagiProps, current: data.currentPage, total: data.totalCount });
      setDataSource(data.pageList);
    };

    const onTableChange = ({ pagination }) => {
      const {pageSize, current: currentPage} = pagination
      getDataSource({ currentPage, pageSize  })
    };

    const tableProps = {
      scroll: { // ModelBody高度 - ModelBody上padding - info高度 - Descriptions title高度 - 表头高度 - 分页器高度
        y: DetailsModelBodyHeight - 24 - height - 42 - 55 - 64
      },
      columns: getDetailsTableColumns(getDataSource),
      rowKey: 'emerNotifierInnerId',
      loading,
      dataSource,
      pagination: pagiProps,
      onTableChange,
    }

    useEffect(() => {
      getDataSource();
    },[])

    return (
        <FCTable {...tableProps}  />
    )
};
export default DetailsTable;